#include "head.h"

void client_handle(ptrain_t pt)
{
    bzero(pt->buf,sizeof(pt->buf));
	while(read(STDIN_FILENO,pt->buf,sizeof(pt->buf)))//从标准输入中读取命令到cmd缓冲中
	{
		pt->data_len=strlen(pt->buf);
		send(pt->fd,&pt->data_len,sizeof(int), 0);//发命令长度给服务器端
		send(pt->fd,pt->buf,pt->data_len,0);
		if(strncmp("cd",pt->buf,2)==0)
		{
			system("clear");
            bzero(pt->buf,sizeof(pt->buf));
			recv(pt->fd,&pt->data_len,sizeof(int),0);//接cd之后的路径
			recvn(pt->fd,pt->buf,pt->data_len);
			printf("%s\n", pt->buf);
		}else if(strncmp("ls",pt->buf,2)==0)
		{
			system("clear");
            bzero(pt->buf,sizeof(pt->buf));
			while(1)
			{
				recv(pt->fd,&pt->data_len,sizeof(int),0);//接文件信息
				if(pt->data_len==0)
				{
					break ;
				}
				recvn(pt->fd,pt->buf,pt->data_len);
				printf("%s\n", pt->buf);
			}
		}else if(strncmp("puts",pt->buf,4)==0)
		{
			char file_name[256];
			bzero(file_name,sizeof(file_name));
			sscanf(pt->buf+5,"%s",file_name);
            if(upload(pt->fd, file_name)==0)
            {
                printf("file-> %s upload success\n",file_name);
            }else 
            {
                printf("file-> %s upload failed\n",file_name);
            }
		}else if(strncmp("gets",pt->buf,4)==0)
		{
			char file_name[256];
			int file_pos =5;
			bzero(file_name,sizeof(file_name));
			while(sscanf(pt->buf+file_pos,"%s",file_name)==1)
			{
				file_pos+=strlen(file_name)+1 ;
				if(download(pt->fd,file_name)==0)
				{
					printf("file -> %s download success \n",file_name);
				}else
				{
					printf("file -> %s download failed \n",file_name);
				}
			}	
		}
		else if(strncmp("rm",pt->buf,2)==0)
		{
			system("clear");
            bzero(pt->buf,sizeof(pt->buf));
			recv(pt->fd,&pt->data_len,sizeof(int),0);
			recvn(pt->fd,pt->buf,pt->data_len);
			printf("%s\n", pt->buf);		
		}else if(strncmp("pwd",pt->buf,3)==0)
		{
			system("clear");
			bzero(pt->buf,sizeof(pt->buf));
			recv(pt->fd,&pt->data_len,sizeof(int),0);
			recvn(pt->fd,pt->buf,pt->data_len);
			printf("%s\n", pt->buf);
		}else 
		{
			printf("wrong cmd!\n");
			continue ;
		}
		bzero(pt->buf,sizeof(pt->buf));
    }
}

int upload(int fd, char* file_name)
{
    int fd_file = open(file_name, O_RDONLY);
	if(fd_file == -1)
	{
		return -1 ;
	}
	char *read_buf = (char*)malloc(Num);
	bzero(read_buf, Num);
	int nread ;
	while(1)
	{
		nread = readn(fd_file, read_buf, Num);
		if(nread < Num)
		{
			send(fd, &nread, sizeof(int), 0);
			sendn(fd, read_buf, nread);
			break ;
		}else
		{
			
			send(fd, &nread, sizeof(int), 0);
			sendn(fd, read_buf, nread);
		}
	}
	int flag = 0 ;
	send(fd, &flag, sizeof(flag), 0);
	close(fd_file);
	return 0 ;
}

int download(int fd, char* file_name)
{
    int fd_file = open(file_name, O_WRONLY|O_CREAT,0666 );
	if(fd_file == -1)
	{
		return -1 ;
	}
	char* write_buf = (char*)malloc(Num);
	bzero(write_buf, Num);
	int nwrite ;
	while(1)
	{
		recv(fd, &nwrite, sizeof(int), 0);
		if(nwrite == 0)
		{
			break ;
		}
		recvn(fd, write_buf, nwrite);
		writen(fd_file, write_buf, nwrite);
	}
	close(fd_file);
	return 0 ;
}

int readn(int fd_read, char* read_buf, int len)
{
	int sum = 0 ;
	int nread ;
	while(sum < len)
	{
		nread = read(fd_read, &read_buf[sum], len - sum);
		if(nread == 0)
		{
			break ;
		}
		sum += nread ;
	}
	return sum ;		
}

int writen(int fd_write, char* write_buf, int len)
{
	int sum = 0 ;
	int nwrite ;
	while(sum < len)
	{
		nwrite = write(fd_write, write_buf + sum, len - sum);
		sum += nwrite ;
	}
	return sum ;
}
